package com.xx.pmis.web.util;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xx.pmis.model.Wine;
import com.xx.pmis.model.util.WineAttach;
import com.xx.pmis.services.WineService;

/**
 * @class 查询所有未结算、未完成 事项<br>
 * 
 * @editor 石星星 @Date 2013-5-3 下午03:30:00 <br>
 * @version jdk_1.5.0
 */
public class WineUtil extends PMISBaseUtil {
	private static final Log log = LogFactory.getLog(WineUtil.class);
	WineService wineService = getPMISServices().getWineService();

	public List<WineAttach> getAllWineAttachByPage(int currentPage, int lineSize) throws Exception {
		List<WineAttach> wineAttachList = new ArrayList<WineAttach>();
		List<Wine> wineList = new ArrayList<Wine>();
		WineAttach wineAttach = null;
		// 分页
		wineList = wineService.getAllWineByConditionByPage(" where wineState = 1 order by wineDate desc", currentPage, lineSize);
		for (Wine wine : wineList) {
			wineAttach = new WineAttach();
			// ID
			wineAttach.setWineID(wine.getWineID());
			// 名称
			wineAttach.setWineName(wine.getWineName());
			// 单瓶进货价
			wineAttach.setPurchasePrice1(wine.getWinePrice());
			// 单瓶优惠
			double preferential1Double = 0.00;
			preferential1Double = getPreferential(wine.getWinePrice(), wine.getWineNum(), wine.getWine1())
					+ getPreferential(wine.getWinePrice(), wine.getWineNum(), wine.getWine2())
					+ getPreferential(wine.getWinePrice(), wine.getWineNum(), wine.getWine3());
			wineAttach.setPreferential1(if1ZeroTo2Zero(new DecimalFormat("####.##").format(preferential1Double)));
			// 单瓶成本
			// 单瓶成本 = 单瓶进货价 - 单瓶优惠
			double cost1Double = Double.parseDouble(wine.getWinePrice()) - preferential1Double;
			wineAttach.setCost1(if1ZeroTo2Zero(new DecimalFormat("####.##").format(cost1Double)));
			// 进货量（瓶）
			wineAttach.setPurchaseAmount1(wine.getWineNum());
			// 总进货款（瓶）
			// 总进货款（瓶） = 单瓶成本 * 进货量（瓶）
			double totalPayment1Double = cost1Double * Integer.parseInt(wine.getWineNum());
			wineAttach.setTotalPayment1(if1ZeroTo2Zero(new DecimalFormat("####.##").format(totalPayment1Double)));
			// 库存量
			wineAttach.setRepertoryNum(wine.getRepertoryNum());
			// 箱规
			wineAttach.setBoxRegulation(wine.getBoxRegulation());

			// 单瓶进货价 string->double
			double purchasePrice1Double = Double.parseDouble(wine.getWinePrice());
			// 箱规 string->int
			int boxRegulationInt = Integer.parseInt(wine.getBoxRegulation());
			// 进货量（瓶）string->int
			int purchaseAmount1Int = Integer.parseInt(wine.getWineNum());
			// 整箱进货价
			String purchasePrice2 = if1ZeroTo2Zero(new DecimalFormat("####.##").format(purchasePrice1Double
					* boxRegulationInt));
			wineAttach.setPurchasePrice2(purchasePrice2);
			// 整箱优惠
			double preferential2Double = 0.00;
			preferential2Double = getPreferential2(purchasePrice2, wine.getWineNum(), wine.getBoxRegulation(), wine
					.getWine1())
					+ getPreferential2(purchasePrice2, wine.getWineNum(), wine.getBoxRegulation(), wine.getWine2())
					+ getPreferential2(purchasePrice2, wine.getWineNum(), wine.getBoxRegulation(), wine.getWine3());
			wineAttach.setPreferential2(if1ZeroTo2Zero(new DecimalFormat("####.##").format(preferential2Double)));
			// 整箱成本
			// 整箱成本 = 整箱进货价 - 整箱优惠
			double cost2Double = Double.parseDouble(purchasePrice2) - preferential2Double;
			wineAttach.setCost2(if1ZeroTo2Zero(new DecimalFormat("####.##").format(cost2Double)));
			// 进货量（箱）
			// 进货量（瓶） / 箱规
			String purchaseAmount2 = "";
			if (purchaseAmount1Int % boxRegulationInt == 0) {// 整箱
				purchaseAmount2 = String.valueOf(purchaseAmount1Int / boxRegulationInt) + "箱";
			} else {// 含零头
				if (purchaseAmount1Int / boxRegulationInt == 0) {// 不到一箱
					purchaseAmount2 = String.valueOf(purchaseAmount1Int % boxRegulationInt) + "瓶";
				} else {// 超过一箱，并且有不满一箱的
					purchaseAmount2 = String.valueOf(purchaseAmount1Int / boxRegulationInt) + "箱零"
							+ String.valueOf(purchaseAmount1Int % boxRegulationInt) + "瓶";
				}
			}
			wineAttach.setPurchaseAmount2(purchaseAmount2);
			// 总进货款（箱）
			// 总进货款（箱） = 整箱成本 * 进货量（箱）
			double totalPayment2Double = cost2Double * purchaseAmount1Int / boxRegulationInt;
			wineAttach.setTotalPayment2(if1ZeroTo2Zero(new DecimalFormat("####.##").format(totalPayment2Double)));
			// 货款平衡
			if (totalPayment1Double == totalPayment2Double) {// 平衡
				wineAttach.setPaymentBalance("0");
			} else {// 不平衡
				wineAttach.setPaymentBalance("1");
			}
			// 单瓶零售价
			wineAttach.setWineRetailPrice1("");
			// 整箱零售价
			wineAttach.setWineRetailPrice2("");
			wineAttachList.add(wineAttach);
		}
		return wineAttachList;
	}

	// 根据输入参数求出优惠金额
	private static double getPreferential(String purchasePrice, String num, String arg) {
		double preferentialDouble = 0.00;
		if (!arg.equals("")) {
			if (arg.substring(0, 1).equals("+")) {
				preferentialDouble = -Double.parseDouble(arg.substring(1)) / Integer.parseInt(num);
			} else if (arg.substring(0, 1).equals("-")) {
				preferentialDouble = Double.parseDouble(arg.substring(1)) / Integer.parseInt(num);
			} else if (arg.substring(0, 1).equals("*")) {// 进货价*优惠比例
				preferentialDouble = Double.parseDouble(purchasePrice) * Double.parseDouble(arg.substring(1));
			} else if (arg.substring(0, 1).equals("/")) {// 
				preferentialDouble = Double.parseDouble(purchasePrice) / Double.parseDouble(arg.substring(1));
			} else {
				log.info("输入参数必须以“+，-，*，/”开始，请检查输入参数！");
			}
		}
		return preferentialDouble;
	}

	// 根据输入参数求出优惠金额
	private static double getPreferential2(String purchasePrice, String num, String boxRegulation, String arg) {
		double preferentialDouble = 0.00;
		if (!arg.equals("")) {
			if (arg.substring(0, 1).equals("+")) {
				preferentialDouble = -Double.parseDouble(arg.substring(1)) / Integer.parseInt(num)
						* Integer.parseInt(boxRegulation);
			} else if (arg.substring(0, 1).equals("-")) {
				preferentialDouble = Double.parseDouble(arg.substring(1)) / Integer.parseInt(num)
						* Integer.parseInt(boxRegulation);
			} else if (arg.substring(0, 1).equals("*")) {// 进货价*优惠比例
				preferentialDouble = Double.parseDouble(purchasePrice) * Double.parseDouble(arg.substring(1));
			} else if (arg.substring(0, 1).equals("/")) {// 
				preferentialDouble = Double.parseDouble(purchasePrice) / Double.parseDouble(arg.substring(1));
			} else {
				log.info("输入参数必须以“+，-，*，/”开始，请检查输入参数！");
			}
		}
		return preferentialDouble;
	}

	/**
	 * 判断金额后面1个0还是2个0，如果1个0或者没有0，则补全到2个0，例如100.1，变成100.10
	 * 
	 * @param str
	 */
	public String if1ZeroTo2Zero(String str) {
		if (str.indexOf(".") != -1) {
			if (str.substring(str.lastIndexOf(".")).length() == 2) {// 1.0
				str = str + "0";
			} else if (str.substring(str.lastIndexOf(".")).length() > 3) {// 1.000,截取小数点后两位
				str = str.substring(0, str.lastIndexOf(".") + 3);
			} else if (str.substring(str.lastIndexOf(".")).length() < 2) {// 1.
				str = str + "00";
			} else {// ==3,1.00
			}
		} else {
			str = str + ".00";
		}
		return str;
	}

	public void formatNullToStr(String arg) {
		if (arg.equals(null) || arg == null) {

		}
	}

	public static void main(String arg[]) {
		String str1 = "-100.00";
		String str2 = "*0.05";
		// String str3 = null;
		int a = 8;
		int b = 6;
		if (a % b == 0) {

		}

		System.out.println("str1:" + str1.substring(0, 1));
		System.out.println("str2:" + str2.substring(1));
		System.out.println("str3:" + a / b);
		System.out.println("str4:" + a % b);
		// System.out.println("str5:" + getPreferential("19.00", str3));
	}

}
